#!/usr/bin/python

import os

DIR = raw_input("Input source dir:")

def getfiles(DIR):

    files = []
    
    for dirname, dirnames, filenames in os.walk(DIR):
            
        for filename in filenames:
            files.append(os.path.join(dirname, filename))
    return files

def get_extension(string):
    # if s contains no "." then extension is NONE
    ext = os.path.splitext(string)[-1][1:].lower()
    # returns last item in list
    if ext == '':
        return "NONE"
    else:
        return ext

def extension_dict(files):
    elist = {}
    for filename in files:
        ext = get_extension(filename)
        if ext in elist:
            elist[ext] += 1
        else:
            elist.update({ext:1})
    return elist


files = getfiles(DIR)

extensions = extension_dict(files)
ext_list = extensions.items()
ext_list.sort(key = lambda item: item[1], reverse = True)

average = sum(extensions.values()) / len(extensions.keys())

ext_list = [item for item in ext_list if item[1] > average] 
ext_list = [(index,ext_list[index][0],ext_list[index][1]) for index in range(0,len(ext_list))]

data = "\n".join([ "%s %s %s" %(num,index,ext) for index, ext, num in ext_list])

print data

f = open("statistics.dat","w")
f.write(data)
f.close()




